function KTHPoseFrequency( datafile, outdir, features )
%KTHPoseFrequency - generate pose-frequency features.

% Examples
%   datafile = '/Users/trananh/Workspace/Python/ua-gesture/data/KTH/ground-truth/pose-std.mat';
%   outdir = '/Users/trananh/Workspace/Python/ua-gesture/data/KTH/ground-truth/pose-features-std/';

  if ~exist('features', 'var')
    features = 0:14;
  end
  
  if ~strcmp(outdir(end), '/')
    outdir = [outdir '/'];
  end
  
  % Frame rate for KTH movies
  Fs = 25;
  
  % Min length of each segment is set to be 2 seconds.
  % The reason is because if the segment is too short, then we won't
  % be able to accurately capture the frequency information.  So for
  % all 'short' segments, we will just repeat the segment to increase
  % the length.
  MININTERVAL = 2 * Fs;
  
  % MINCOMP controls how much of the spectrum we will consider.
  MINCOMP = round(MININTERVAL/2);
  
  % Load pose data
  load(datafile, 'KTHdata');

  % Iterate over each feature
  fprintf('Extracting features from %s\n\n', datafile);
  for f=1:numel(features)
    fidx = features(f);
    
    % Open file for write
    [~, fdesc] = generatePoseFeatures(0, 1, fidx, {});
    fid = fopen([outdir fdesc '.txt'], 'w');
    fprintf(' - Working on feature: %s\n', fdesc);
  
    % Iterate over each action
    for i=1:numel(KTHdata.actions)
      actionset = KTHdata.actions(i);
      actionname = actionset.actionname;
      fprintf('   * %s [progress: person_00]', actionname);

      % Iterate over each person
      for j=1:numel(actionset.persons)
        personset = actionset.persons{j};
        personid = num2str(j);
        fprintf('\b\b\b%02d]', j);

        % Iterate over each clip
        for k=1:numel(personset)
          clipset = personset(k);
          clipname = clipset.clipname;

          % Finally, iterate over each segment for each clip
          for m=1:numel(clipset.segments)
            segment = clipset.segments(m);
            segmentid = segment.segmentid;

            % Generate frequency features
            pose = segment.pose;
            if ~isempty(pose)

              % Doctor up the segment if it is too 'short'
              if size(pose, 1) < MININTERVAL
                rep = ceil(MININTERVAL / size(pose,1));
                pose = repmat(pose,rep,1);
              end
              assert(size(pose,1) >= MININTERVAL);

              % Compute frequency spectrum
              freq = generatePoseFrequency(double(pose), ...
                KTHdata.poseheaders, fidx, Fs);
              freq = abs( freq ).^2;

              % Trim to take only the top MIN components
              freq = freq(:,2:2+MINCOMP);
              freq = reshape(freq.',1,[]);

              % Write to file
              fprintf(fid, '%s,%s,%s,%s,', actionname, personid, ...
                clipname, segmentid);
              for n=1:length(freq)-1
                fprintf(fid, '%s,', num2str(freq(n)));
              end
              fprintf(fid, '%s\n', num2str(freq(end)));
            
            end
            
          end

        end

      end
      
      fprintf('\n');
    end
  
    % Close file
    fclose(fid);
  
  end
end

